var _grid = $('#jgTable'); 
gridWidth = $('.u_toolbar');

var checked = [], mData = [];

$(function(){

    _gridInit();

    $("li[data-opt='opt_search']").find(">i").on('click', function(){
        gridSearch.Content = {
            SearchText: $("#opt_search").find(">input").val()
        };
        search();
    });

    $('button[data-opt="u_check"]').on('click', saveCheck);

});

function _gridInit(){   //OMS001202
    var _gridData = Object.create(gridData);
    _gridData.url = omsPath + '/OMS0012/OMS001202';
    _gridData.colModel = [
        {label:'订单号', name:'OrderNo'},
        {label:'客户代码', name:'CardCode'},
        {label:'客户名称', name:'CardName'},
        {label:'用户代码', name:'UserCode'},
        {label:'用户名称', name:'UserName'},
        {label:'下单时间', name:'CreateDate'},
        {label:'更新时间', name:'UpdateDate', hidden:true},
        {label:'单据总计', name:'DocTotal'},
        {label:'单据状态', name:'DocStatus', hidden:true},
        {label:'物料数', name:''},
        {label:'备注', name:'Comment'},
        {label:'开票公司名称', name:'TaxCompany', hidden:true},
        {label:'开票类型', name:'TaxType'},
        {label:'开票开户行', name:'TaxBank', hidden:true},
        {label:'开票账号', name:'TaxAccount', hidden:true},
        {label:'开票税号', name:'TaxNumber', hidden:true},
        {label:'开票地址', name:'TaxAddress', hidden:true},
        {label:'开票电话', name:'TaxPhone', hidden:true},
        {label:'付款方式', name:'Paymentmethod', hidden:true},
        {label:'付款状态', name:'PaymentStatus', hidden:true},
        {label:'物流状态', name:'LogisticStatus', hidden:true},
        {label:'发票状态', name:'TaxStatus', hidden:true},
        {label:'已提交支付金额', name:'ReqPayTotal', hidden:true},
        {label:'已支付金额', name:'PayTotal', hidden:true},
        {label:'收货地址ID', name:'AddressId'},
        {label:'省', name:'Province'},
        {label:'地区', name:'Area'},
        {label:'市', name:'City'},
        {label:'街道', name:'Street'},
        {label:'收货详细地址', name:'Address'},
        {label:'收货人', name:'Receiver'},
        {label:'收货人电话', name:'ReceiverPhone'}
    ];
    _gridData.sortname = 'OrderNo';
    var _postData = {
        Method: 'Q',
        Content:{
            SearchText: ''
        }
    };
    for(i in postData){
        _postData[i] = postData[i];
    }
    //console.log(postData)
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            postData : _postData,
        });
    };
    _gridData.loadComplete = function(xhr){
        xhr.Status == -999 && alert_QX($('body'), xhr.Message);
        var rowData = _grid.jqGrid('getRowData');
        mData = mData.concat(rowData).unique('OrderNo');
        rowData.forEach(function(v,i,a){
            checked.indexOf(v.OrderNo)!=-1 
            && _grid.jqGrid('setSelection',i+1);
        });
    };
    _gridData.onSelectRow = function(rowid,status){
        //console.log(rowid, status)
        var rowData = _grid.jqGrid('getRowData', rowid);
        var rowNo = rowData.OrderNo;
        if(status){  
            checked.indexOf(rowNo) == -1 
            && checked.push(rowNo);
        }else{
            checked.splice(checked.indexOf(rowNo),1);
        }
        console.log(checked)
    };
    _gridData.multiselect = true;
    _gridData.onSelectAll = function(rowids,status){
        var rowData = _grid.jqGrid('getRowData');
        if(status){
            rowData.forEach(function(v,i,a){
                checked.indexOf(v.OrderNo) == -1 
                && checked.push(v.OrderNo);
            });
        }else{
            rowData.forEach(function(v,i,a){
                checked.indexOf(v.OrderNo) != -1 
                && checked.splice(checked.indexOf(v.OrderNo),1);
            });
        }
        console.log(checked);
    };
    _gridData.ondblClickRow = function(rowid,iRow,iCol,e){
        editCode(rowid);
    };
    gridInit(_gridData);
    gridSearch = _postData;
}

//Options
function editCode(rowId){
    if(rowId){  //判断是否选中有效行
        var rowData = _grid.jqGrid('getRowData', rowId);
        window.open('OMS001202.html?OrderNo=' + rowData.OrderNo, '_self');
    }else{
        myAlert('请选择您要编辑的数据');
    }   
}
function saveCheck(){   //OMS001206
    var _requestData = {
        // Method: 'A'
    };
    for(i in postData){
        _requestData[i] = postData[i];
    }
    var tempArr=[];
    mData.forEach(function(v,i,a){
        if(checked.indexOf(v.OrderNo) != -1){
            tempArr.push({OrderNo:v.OrderNo, UserCode:v.UserCode})
        }
    });
    _requestData.Content = tempArr;
    console.log(_requestData);
    $.ajax({
        type: "post",
        url: omsPath+'/OMS0012/OMS001206',
        data: _requestData,
        dataType: "json",
        success: function (re) {
            if (re.Status == 200) {
                myAlert('审核成功');
                _grid.trigger('reloadGrid');
                checked = [], mData = [];
            } else {
                myAlert(re.Message);
            }
        }
    });   
}